﻿using CommonTools;
using Communicate;
using HY_UPHMgr;
using StationCommunication;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using UserData;

namespace StationDemo
{
    public class LoadSeverMgr
    {
        private static LoadSeverMgr instance = null;

        private static readonly Object _lock = new object();

        public static LoadSeverMgr Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        instance = new LoadSeverMgr();
                    }
                }
                return instance;
            }
        }


        public LoadServer server = new LoadServer("治具1");  
        private string name = "Server";
       
        public void Init()
        {
            server.Run("127.0.0.1", 8000);
         
        }

        public void Start()
        {

            server.Cmd_运控平台通知标定平台启动();

        }


        public void Stop()
        {
            server.Cmd_运控平台通知标定平台停止();

        }

        public void Functio_停止取图()
        {
            int re = 0;
            string cmd = $"{(int)ServerCmd.关闭点亮}";
            re = server.Cmd_运控平台通知标定平台测试(cmd);
            if (re == 0)
            {
                LogHelper.OnTest_ShowLog(name, $"{server.name}点亮停止成功");

            }
            else
            {
                WaranResult result = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.通讯超时, new string[] { "确定" }, new string[] { $"{server.name}通讯超时，请检查通讯是否连接" });
            }
        }
        public void Function_通知标定开始测试()
        {
            int re = 0;
            if (server.Function_IsConnect())
            {
                re = server.Cmd_运控平台通知标定平台启动();
                if (re == 0)
                {
                    LogHelper.OnTest_ShowLog(name, $"启动成功");

                }
                else
                {
                    WaranResult result = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.通讯超时, new string[] { "确定" }, new string[] { $"{server.name}通讯超时，请检查通讯是否连接" });
                }

            }
        }
        public void Function_通知标定停止测试()
        {
            int re = 0;
            if (server.Function_IsConnect())
            {
                re = server.Cmd_运控平台通知标定平台停止();
                if (re == 0)
                {
                    LogHelper.OnTest_ShowLog(name, $"{server.name}停止成功");

                }
                else
                {
                    WaranResult result = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.通讯超时, new string[] { "确定" }, new string[] { $"{server.name}通讯超时，请检查通讯是否连接" });
                }

            }

        }


        #region 三角测量
        public bool Function_点亮(string stringSN)
        {
            string cmd = $"{(int)ServerCmd.点亮},{stringSN}";
          
            int res = 0;
          
        retry:
            res = server.Cmd_运控平台通知标定平台测试(cmd,Param_System.Instance.点亮通讯超时时间._strParamVal);

            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.点亮超时, new string[] { "重试", "点亮失败，换料" }, new string[] { "点亮超时，请选择重新点亮或者换料测试" });
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {
                    return false;
                }

            }
            else if (res != 0)
            {
               
                return false;
            }
            return true;

        }
        public bool Function_读取标定数据()
        {
            string cmd = $"{(int)ServerCmd.读取烧录数据}";

            int res = 0;

        retry:
            res = server.Cmd_运控平台通知标定平台测试(cmd, Param_System.Instance.读取标定数据超时时间._strParamVal);

            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.读取标定数据超时, new string[] { "重试", "换料" }, new string[] { "读取标定数据超时，请选择重新读取或者换料测试" });
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {
                    return false;
                }

            }
            else if (res != 0)
            {

                return false;
            }
            return true;

        }
        public bool Function_三角取图(int index)
        {
            int res = 0;
            string cmd = $"{(int)ServerCmd.三角取图},{index}";
        retry:
            res = server.Cmd_运控平台通知标定平台测试(cmd);
            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.取图超时, new string[] { "重试", "NG" }, new string[] { "取图超时超时，请选择重新取图或者取图NG" }, "None");
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {

                    return false;
                }
            }
            if (res != 0)
            {

                return false;
            }
            return true;

        }
        public bool Function_三角标定(ref double err)
        {
            try
            {
                string cmd = $"4";

                int res = 0;
                string result = "";
            retry:
                res = server.Cmd_运控平台通知三角平台测试并返回结果(cmd, ref result, Param_System.Instance.点亮通讯超时时间._strParamVal);

                if (res == -2)
                {
                    WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.读取标定数据超时, new string[] { "重试", "换料" }, new string[] { "读取标定数据超时，请选择重新读取或者换料测试" });
                    if (waranResult == WaranResult.Custom1)
                    {
                        goto retry;
                    }
                    else
                    {
                        return false;
                    }

                }
                else if (res != 0)
                {

                    return false;
                }

                if (result != "")
                {
                    err = Convert.ToDouble(result);
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.OnTest_ShowLog(Station.上料.ToString(), $" 发生错误:{ex.StackTrace},{ex.Message}", LogType.Err);
                return false;
            }
        }

        #endregion


        #region  标定


        public bool Function_清空标定数据()
        {
            int res = 0;
            string cmd = $"{(int)ServerCmd.清标定数据}";
        retry:
            res = server.Cmd_运控平台通知标定平台测试(cmd);
            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.通讯超时, new string[] { "确定" }, new string[] { $"清空标定数据通讯超时，请检查通讯是否连接" }, "None");
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {

                    return false;
                }
            }
            if (res != 0)
            {

                return false;
            }
            return true;

        }
        public bool Function_取图(int index)
        {
            int res = 0;
            string cmd = $"{(int)ServerCmd.取图},{index}";
        retry:
            res = server.Cmd_运控平台通知标定平台测试(cmd);
            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.取图超时, new string[] { "重试", "NG" }, new string[] { "取图超时超时，请选择重新取图或者取图NG" }, "None");
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {

                    return false;
                }
            }
            if (res != 0)
            {

                return false;
            }
            return true;

        }

        public bool Function_点亮( string stringSN, ref double voltage, ref double current,int Timeout = 10000)
        {
            string cmd = $"{(int)ServerCmd.点亮},{stringSN}";

            int res = 0;
            string result = "";
        retry:
            res = server.Cmd_运控平台通知标定平台测试并返回结果(cmd, ref result, Timeout);

            if (res == -2)
            {
                WaranResult waranResult = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.点亮超时, new string[] { "重试", "点亮失败，换料" }, new string[] { "点亮超时，请选择重新点亮或者换料测试" }, "None");
                if (waranResult == WaranResult.Custom1)
                {
                    goto retry;
                }
                else
                {
                    return false;
                }

            }
            else if (res != 0)
            {

                return false;
            }
            string[] str2 = result?.Split(',');
            if (str2.Length >= 2)
            {
                voltage = str2[0].ToDouble();
                current = str2[1].ToDouble();
            }

            return true;

        }


        #endregion

    }
}
